package com.fivebrms.hibernate.entity.business;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.Iterator;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fivebrms.forms.w9.forms.RemoteConfidentialityForm;
import com.fivebrms.forms.w9.forms.RemoteLoginCredentials;
import com.fivebrms.hibernate.entity.dao.ConfidentialityFormDaoHibernate;
import com.fivebrms.hibernate.entity.dao.LoginDaoHibernate;
import com.fivebrms.hibernate.entity.model.ConfidentialityForm;
import com.fivebrms.hibernate.entity.model.LoginForm;

public class ConfidentialityService
{
	final Logger	log	= LoggerFactory.getLogger(ConfidentialityService.class);

	public RemoteConfidentialityForm submitConfidentialityForm(RemoteConfidentialityForm remoteConfidentialityForm)
	{
		log.info("login..ok successfully called the submitConfidentialityForm.. " + remoteConfidentialityForm);

		ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext
		        .getServletConfig().getServletContext());

		log.info("form created..2..");

		String confidentialitySpringBean = "confidentialityDAO";

		ConfidentialityFormDaoHibernate confidentialityDAO = (ConfidentialityFormDaoHibernate) appContext.getBean(confidentialitySpringBean);

		ConfidentialityForm confidentialityForm = new ConfidentialityForm();
		try
		{
			BeanUtils.setProperty(confidentialityForm, "vendorName", remoteConfidentialityForm.getVendorName() );
			BeanUtils.setProperty(confidentialityForm, "vendorCode", remoteConfidentialityForm.getVendorCode() );
			BeanUtils.setProperty(confidentialityForm, "dateSubmitted", new Timestamp(GregorianCalendar.getInstance().getTimeInMillis() ) );
		}
		catch (IllegalAccessException iae)
		{
			log.error("an error IllegalAccessException...", iae);
		}
		catch (InvocationTargetException ite)
		{
			log.error("an error occurred...", ite);
		}
		//
		// place the object into the database
		//
		confidentialityDAO.createConfidentialityForm(confidentialityForm);

		//
		// for now all confidentiality credentials are successfully submitted
		//
		remoteConfidentialityForm.setSubmitted(true);
		
		log.info("login..finished confidentiality successfully.. ");

		log.info("login looking..size-->" + confidentialityDAO.findAll().size());

		return remoteConfidentialityForm;
	}


}
